package com.itheima.server.controller;

import com.itheima.commons.utils.JwtUtils;
import com.itheima.dubbo.api.UserInfoApi;
import com.itheima.model.domain.UserInfo;
import com.itheima.model.vo.PageResult;
import com.itheima.model.vo.UserInfoVo;
import com.itheima.server.interceptor.UserHolder;
import com.itheima.server.service.TanhuaService;
import com.itheima.server.service.UserInfoService;
import com.itheima.server.service.UserService;
import io.jsonwebtoken.Claims;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private TanhuaService tanhuaService;

    /*获取用户信息
     * UserId可以没有，有的话就是查询UserId的信息，
     * 没有的话就查询登陆的用户信息
     * */
    @GetMapping
    public ResponseEntity users(Long UserId) {

        //判断UserId是否存在
        UserId = UserId == null ? (UserHolder.getUserId()) : UserId;
        UserInfoVo userInfoVo = userInfoService.findById(UserId);
        return ResponseEntity.ok(userInfoVo);
    }

    /*
     * 更新用户资料
     *
     * */
    @PutMapping
    public ResponseEntity update(@RequestBody UserInfo userInfo) {
        Long id = UserHolder.getUserId();
        userInfo.setId(id);
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /*互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表*/
    @GetMapping("/friends/{type}")
    public ResponseEntity friendsType(@PathVariable("type") Integer type,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pagesize,
                                      String nickname) {
        PageResult pr = userService.findFriends(type, page, pagesize, nickname);
        if (pr == null) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.ok(pr);
    }


    //喜欢取消
    @DeleteMapping("/like/{uid}")
    public ResponseEntity unlove(@PathVariable("uid") Long likeUserId) {
        tanhuaService.notlove(likeUserId);
        return ResponseEntity.ok(null);
    }
    //粉丝喜欢
    @PostMapping("/fans/{uid}")
    public ResponseEntity love(@PathVariable("uid") Long likeUserId) {
        tanhuaService.love(likeUserId);
        return ResponseEntity.ok(null);
    }
    @GetMapping("/{uid}/alreadyLove")
    public ResponseEntity alreadyLove(@PathVariable("uid") Long liekUserId){
      return  userService.alreadyLove(liekUserId)?ResponseEntity.ok(true):ResponseEntity.ok(null);
    }
}
